﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernateDB;

// .dll name has to be the same as the namespace
namespace TemperatureFilter
{
    // Temperature filter
    public class Filter : IFilter
    {
        private const int FILTER_ID = 1001;
        private const string FILTER_VER = "1.0";
        private const string FILTER_DESC =
            "Temperature filter.\n" +
            "This filter is used to transfer files.";

        // variable used for temperature calculations
        private double tempSum = 0;
        private int tempCount = 0;
        private double minTemp = 0;
        private double maxTemp = 0;

        // server functions
        private ServerASISTSYS.ServerFunctions sf;

        #region IFilter Members

        public int GetID()
        {
            return FILTER_ID;
        }

        public string GetVersion()
        {
            return FILTER_VER;
        }

        public string GetDescription()
        {
            return FILTER_DESC;
        }

        public void Parse(int length, int id, byte[] data)
        {
            try
            {
                string str = "";
                string Date = "";
                string Hour = "";
                double Value = 0;

                for (int i = 0; i < length; i++)
                {
                    str = str + (char)data[i];
                }

                Console.WriteLine("Data from TEMP sensor: " + str);

                if (str.Length > 0)
                {
                    Value = Double.Parse(str.Substring(0, 5).Trim());
                    Date = str.Substring(5, 11).Trim();
                    Hour = str.Substring(16, 8).Trim();
                }

                //Console.WriteLine("Value: " + Value);
                //Console.WriteLine("Date : " + Date);
                //Console.WriteLine("Hour : " + Hour);

                //Human temperature calculations
                //Human min temperature       
                if (Value < 36.12)
                {
                    minTemp = Value;
                    sf.SendAlertMessage(sf.CNP, id, "MIN TEMP " + minTemp);
                }
                //Human max temperature  
                if (Value > 37.5)
                {
                    maxTemp = Value;
                    sf.SendAlertMessage(sf.CNP, id, "MAX TEMP " + maxTemp);
                }
                //Human average temperature
                tempSum = tempSum + Value;
                tempCount++;
                double tempMed = tempSum / tempCount;

                //Console.WriteLine("minTemp : " + minTemp);
                //Console.WriteLine("maxTemp : " + maxTemp);
                //Console.WriteLine("tempMed : " + tempMed);

                // reset sensors minimum and maximum values.
                if (minTemp > 0)
                {
                    //Console.WriteLine("Valoarea minima: " + minTemp);
                    minTemp = 0;
                }
                if (maxTemp > 0)
                {
                    //Console.WriteLine("Valoarea maxima: " + maxTemp);
                    maxTemp = 0;
                }


                // adaugare la BD
                SenzoriTemperatura st = new SenzoriTemperatura();

                st.IdSenzor = id;
                st.IdPacient = sf.CNP;
                st.Temperatura = Value;
                st.Data = DateTime.Parse(Date + " " + Hour);

                PersistenceManager pm = PersistenceManager.Instance;
                pm.Save(st);
            }
            catch (FormatException)
            {
                // the message could not be processed
                Console.WriteLine("The format of the string is incorect!");
            }
            catch// (Exception x)
            {
                ;
            }
        }

        public bool Validate()
        {
            return true;
            //throw new NotImplementedException();
        }

        public void SetServerFunctions(object sf)
        {
            this.sf = (ServerASISTSYS.ServerFunctions)sf;
        }

        public bool CanProcess(int id)
        {
            switch (id)
            {
                case 100001:
                case 100002:
                    return true;
                default:
                    return false;
            }
        }
        #endregion
    }
}
